﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Speech.Recognition;
using System.Text;

namespace DigiMate
{
	public class BaseService
	{
		protected bool inProcess = false;
		protected int cutLongSummaryTo = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["CutLongSummaryTo"]); //letters!
		
		protected CultureInfo Culture = new CultureInfo("en-us");
		protected SpeechRecognitionEngine reco = null;
		protected RecognizerInfo recognizerInfo = null;


		public void RunService(string text)
		{
			if (DigiMate.AnyServiceActive)
			{
				AddLog("Some service already running");
				return;
			}

			try
			{
				DigiMate.AnyServiceActive = true;
				Run(text);
			}
			finally
			{
				// For async services we should set "DigiMate.AnyServiceActive = false" manually at that service!
				DigiMate.AnyServiceActive = false;
			}	
		}

		public virtual void Run(string text)
		{
			
		}

		protected virtual bool IsAsync
		{
			get { return false; }
		}
		
		protected BaseService()
		{
			GetReco();
		}

		protected void AddLog(string text)
		{
			Console.WriteLine(DateTime.Now.ToString("HH:mm:ss") + " (" + this.GetType().Name + ") " + text);
		}


		public SpeechRecognitionEngine GetReco()
		{
			reco = SpeechReco.GetReco();
			LoadGrammar();
		
			return reco;
		}

		protected virtual void LoadGrammar()
		{
			reco.LoadGrammar(GetGrammar());
		}

		protected virtual Grammar GetGrammar()
		{
			GrammarBuilder grammarBuilder = new GrammarBuilder("test");
			grammarBuilder.Culture = reco.RecognizerInfo.Culture;
			return new Grammar(grammarBuilder); // load grammar
		}

	}
}
